/**
 * Created by yuris on 06.02.15.
 */
package InterfaceObjects {
import com.greensock.TweenMax;
import com.greensock.easing.Back;
import com.greensock.easing.BackIn;
import com.greensock.easing.Bounce;
import com.greensock.easing.BounceIn;
import com.greensock.easing.BounceOut;
import com.greensock.easing.Cubic;
import com.greensock.easing.Ease;
import com.greensock.easing.Expo;
import com.greensock.easing.Quad;

import feathers.controls.Button;

import flash.geom.Rectangle;

import starling.display.Image;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;

public class ButtonNavigation extends Button {
    public static const BUTTON_TYPE_STEP_LEFT:String = "BUTTON_TYPE_STEP_LEFT";
    public static const BUTTON_TYPE_STEP_RIGHT:String = "BUTTON_TYPE_STEP_RIGHT";
    public static const BUTTON_TYPE_REWIND_LEFT:String = "BUTTON_TYPE_REWIND_LEFT";
    public static const BUTTON_TYPE_REWIND_RIGHT:String = "BUTTON_TYPE_REWIND_RIGHT";

    private var _skinImage:Image;
    private var _type:String = "";
    private var _direction:int = 1;

    public function ButtonNavigation(type:String = BUTTON_TYPE_REWIND_LEFT) {
        super();
        this._type = type;
        switch (type){
            case BUTTON_TYPE_STEP_LEFT:
                this._skinImage = GraphicFactory.getImageByName("Interface/Signs/sign_triangle_big");
                this._direction = -1;
                break;
            case BUTTON_TYPE_STEP_RIGHT:
                this._skinImage = GraphicFactory.getImageByName("Interface/Signs/sign_triangle_big");
                //this._skinImage.rotation = Math.PI;
                this._skinImage.scaleX = -1;
                break;
            case BUTTON_TYPE_REWIND_LEFT:
                this._skinImage = GraphicFactory.getImageByName("Interface/Signs/sign_rewind");
                this._direction = -1;
                break;
            case BUTTON_TYPE_REWIND_RIGHT:
                this._skinImage = GraphicFactory.getImageByName("Interface/Signs/sign_rewind");
                //this._skinImage.rotation = Math.PI;
                this._skinImage.scaleX = -1;
                break;
        }

        this.addEventListener(Event.TRIGGERED, HANDLER_triggered);

        //this._hitArea = new Rectangle(-this._skinImage.width, -this._skinImage.height, this._skinImage.width*2, this._skinImage.height*2);
        this._skinImage.alignPivot();
        this.defaultSkin = this._skinImage;
        this.useHandCursor = true;
    }

    private var _bufTouch:Touch;

    private var _bufX:int;
    private function HANDLER_triggered(event:Event):void {
       // this._bufTouch = event.getTouch(this);
       // if(this._bufTouch && this._bufTouch.phase == TouchPhase.BEGAN){
        if(TweenMax.isTweening(this)){
            return;
        }
        this._bufX = this.x;
            if(this._type.indexOf("STEP") >= 0){
                TweenMax.to(this,0.15, {x: this._bufX + 15*this._direction, yoyo:true, repeat:1, ease: Quad.easeOut});
            } else {
                TweenMax.to(this,0.75, {x: this._bufX + 15*this._direction, ease: Bounce.easeOut, onComplete: this._easeBack});
            }

       // }

    }



    private function _easeBack():void {
        TweenMax.to(this,0.35, {x: this._bufX, ease: Back.easeOut});
    }


}
}
